﻿//编辑对话框
var dlg = new MyAppDialog(900, 600);

//列定义
var cols = [[
    { field: "Id", title: 'Id', width: 10, hidden: true },
    { field: "ck", title: '', width: 20, checkbox: true },
    { field: "Username", title: '用户名', sortable: true, width: 100, align: 'center' },
    { field: "Realname", title: '姓名', sortable: true, width: 100, align: 'center' },
    { field: "Gender", title: '性别', sortable: true, width: 40, align: 'center' },
    {
        field: "Islocked", title: '状态', sortable: true, width: 40, align: 'center', formatter: function (value, rec) {
            if (value == "1")
                return "<div class='helper-font-16'><i title='停用' class='iconfont icon-close1' style='color:red;'></i></div>";
            else
                return "<div class='helper-font-16'><i title='可用' class='iconfont icon-check1' style='color:green;'></i></div>";
        }
    },
    { field: "Lastlogintime", title: '最后登录', sortable: true, width: 100, align: 'center' },
    { field: "Logintimes", title: '登录次数', sortable: true, width: 40, align: 'center', formatter: function (value, rec) { if (value == "0") return ""; else return "<a href='#none'>" + value + "</a>"; } }
]];


$(document).ready(function () {


    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.siteid = $("#Siteid").val();
        $("#datagrid").datagrid("load");
    });
    //取消
    $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });


    LoadDatagrid();

    //if (!canEdit) {
    //    $("#datagrid").datagrid('showColumn', 'op');
    //    // $("#datagrid").datagrid('showColumn', 'locked');
    //}
});

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '用户列表',
        url: baseurl + 'Manage/User/UserListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: "",
            siteid: 0,
            roleid: Roleid
        },
        sortName: 'Id',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setSize(900, 600);
            dlg.setURL("编辑用户信息", baseurl + "Manage/User/Edit/" + row.Id + "?" + Math.random());
            dlg.show();
        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //}
    });
    //PaginationInit();
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    $.messager.confirm(csms_lang.message_confirm, csms_lang.message_delete_confrim, function (r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < row.length; i++) {
                ids += row[i].Id + ",";
            }

            $.ajax({
                type: "post",
                url: baseurl + "Manage/User/Delete",
                data: { ids: ids },
                success: function (e) {
                    if (e.Ret == "0") {
                        $("#datagrid").datagrid("reload");
                    } else {
                        $.messager.alert(csms_lang.message_error, e.Msg, "error");
                    }
                }
            });
        }
    })

}

//重置密码
function retsetFun(obj) {
    dlg.setSize(500, 320);
    dlg.setURL("重置密码", baseurl + "Manage/User/Resetpwd/" + $(obj).attr("data-id") + "?" + Math.random());
    dlg.show();
}

//锁定//解锁
function IslockedFun(obj, sign) {
    if (sign == "lock") {
        $.messager.confirm(csms_lang.message_confirm, "确认要禁用此账号吗？", function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: baseurl + "Manage/User/Islockedpwd" + "?" + Math.random(),
                    data: { ids: $(obj).attr("data-id") },
                    success: function (e) {
                        if (e.Ret == "0") {
                            $("#datagrid").datagrid("reload");
                        } else {
                            $.messager.alert(csms_lang.message_error, e.Msg);
                        }
                    }
                });
            }
        });
    }
    else {
        $.messager.confirm(csms_lang.message_confirm, "确认要启用此账号吗？", function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: baseurl + "Manage/User/Islockedpwd" + "?" + Math.random(),
                    data: { ids: $(obj).attr("data-id") },
                    success: function (e) {
                        if (e.Ret == "0") {
                            $("#datagrid").datagrid("reload");
                        } else {
                            $.messager.alert(csms_lang.message_error, e.Msg, "error");
                        }
                    }
                });
            }
        });
    }
}




